package com.miaomiao.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaomiao.domain.system.*;
import com.miaomiao.mapper.system.MenuMapper;
import com.miaomiao.service.system.MenuService;
import com.miaomiao.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;
/*    @Override
    public List<Menu> getMenuList() {
        Long userId = SecurityUtils.getUserId();
        List<Menu> menus = menuMapper.selectMenuByUserId(userId);
        List<Menu> collect = menus.stream().map(item -> {
            if(item.getParentId() == 0) return item;
            return null;
        }).collect(Collectors.toList());
        return collect;
    }*/

    @Override
    public List<Menu> selectMenuTreeByUserId(Long userId) {
        List<Menu> menus;
        if (SecurityUtils.isAdmin(userId))
        {
            menus = menuMapper.selectMenuAll(new Menu());
        }
        else
        {
            menus = menuMapper.selectMenuTreeByUserId(userId);
        }
        return getChildPerms(menus, 0);
    }

    @Override
    public List<Menu> selectMenuTreeByUserId(Menu menu,Long userId) {
        List<Menu> menus;
        if (SecurityUtils.isAdmin(userId))
        {
            menus = menuMapper.selectMenuAll(menu);
        }
        else
        {
            Map<String, Object> map = new HashMap<>();
            map.put("userId",userId);
            menu.setParams(map);
            menus = menuMapper.selectMenuByUserId(menu);
        }
        return getChildPerms(menus, 0);
    }

    @Override
    public List<Router> buildMenus(List<Menu> menus) {
        List<Router> routers = new ArrayList<>();
        for (Iterator<Menu> it = menus.iterator(); it.hasNext();){
            Menu menu = it.next();
            if("*:*:*".equals(menu.getPerms())) continue;
            Router router = new Router();
            router.setVisible(menu.isVisible());
            router.setPath(menu.getPath());
            if(menu.getComponent() != null) {
                router.setComponent(menu.getComponent());
            }
            router.setName(StringUtils.capitalize(menu.getPath().replace("/","")));
            router.setMeta(new Meta(menu.getMenuName(),menu.getIcon()));
            if(menu.getChildren().size() > 0){
                router.setChildren(buildMenus(menu.getChildren()));
            }
            routers.add(router);
        }
        return routers;
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<Menu> getChildPerms(List<Menu> list, int parentId)
    {
        List<Menu> returnList = new ArrayList<>();
        for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
        {
            Menu t = (Menu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId)
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    @Override
    public boolean addMenu(Menu menu) {
        int insert = menuMapper.insert(menu);
        Boolean aBoolean = menuMapper.addRoleMenu(1L, menu.getMenuId());
        if(insert > 0 && aBoolean) return true;
        return false;
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t 子节点
     */
    private void recursionFn(List<Menu> list, Menu t)
    {
        // 得到子节点列表
        List<Menu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Menu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Menu> getChildList(List<Menu> list, Menu t)
    {
        List<Menu> tlist = new ArrayList<>();
        Iterator<Menu> it = list.iterator();
        while (it.hasNext())
        {
            Menu n = it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Menu> list, Menu t)
    {
        return getChildList(list, t).size() > 0;
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<Menu> menus) {
        return menus.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Menu> getRoleMenus(Long roleId) {
        return menuMapper.getRoleMenus(roleId);
    }
}
